//
//  BankListController.m
//  EasyCar
//
//  Created by nsm on 2018/1/9.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "BankListController.h"

@interface BankListCell : NSMTableViewCell {
    UIImageView *_iconImageView;
    UILabel     *_titleLabel;
}
@end

@implementation BankListCell
- (void)setObject:(id)obj {
    [_iconImageView setImageURL:obj[@"bank_image"]];
    _titleLabel.text = SafeString(obj[@"bank_name"]);
}

- (void)setupSubviews {
    MyLinearLayout *layout = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
    layout.backgroundColor = [UIColor whiteColor];
    layout.myHorzMargin = 0;
    layout.myHeight = 48;
    layout.padding = UIEdgeInsetsMake(0, 15, 0, 15);
    layout.subviewHSpace = 12;
    layout.gravity = MyGravity_Vert_Center;
    layout.cacheEstimatedRect = YES;
    [self.contentView addSubview:layout];
    self.rootLayout = layout;
    
    _iconImageView = [UIImageView new];
    _iconImageView.mySize = CGSizeMake(40, 40);
    _iconImageView.contentMode = UIViewContentModeScaleAspectFill;
    [layout addSubview:_iconImageView];
    
    _titleLabel = [UILabel new];
    _titleLabel.weight = 1;
    _titleLabel.wrapContentHeight = YES;
    _titleLabel.numberOfLines = 1;
    _titleLabel.font = CustomFont(16);
    _titleLabel.textColor = [UIColor darkGrayColor];
    [layout addSubview:_titleLabel];
}
@end

@interface BankListController ()
@end

@implementation BankListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatTableView];
}

- (void)creatTableView {
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-kNavBarHeight);
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 15+40+12, 0, 0);
    
    self.tableView.estimatedRowHeight = 48;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.tableHeaderView = [UIView new];
    
    self.tableView.urlString = @"publics/bank.html";
    self.tableView.headerRefreshEnabled = YES;
    
    @nsm_weak(self)
    self.tableView.sourceManager.classForCellAtIndexPath = ^(NSIndexPath *indexPath) {
        return [BankListCell class];
    };
    self.tableView.didRefreshingHeaderSuccess = ^(id responseObject) {
        @nsm_strong(self)
        id data = ((NSMResponseObject *)responseObject).data;
        self.tableView.sourceManager.dataArray = [NSMutableArray arrayWithArray:data[@"list"]];
    };
    self.tableView.didRefreshingHeaderEmpty = ^(id responseObject) {
        @nsm_strong(self)
        self.tableView.imageForEmptySet = kEmptyDataImage;
        self.tableView.titleForEmptySet = kEmptyDataDesc(((NSMResponseObject *)responseObject).desc);
    };
    
    [self.tableView beginRefreshing:RefreshHudDefault];
}

#pragma mark - TableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *item = tableView.sourceManager.dataArray[indexPath.row];
    !self.didSelected?:self.didSelected(item);
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
